package cn.kgc.service.impl;

import cn.kgc.base.SystemConstant;
import cn.kgc.entity.ReportJobBo;
import cn.kgc.entity.ReportJobMO;
import cn.kgc.repository.ReportJobRepository;
import cn.kgc.service.ReportJobService;
import cn.kgc.utils.PagedGridResult;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.function.Supplier;

/**
 * @author YC
 */
@Service
public class ReportJobServiceImpl implements ReportJobService {

    @Resource
    private ReportJobRepository reportJobRepository;
    @Resource
    private MongoTemplate mongoTemplate;

    @Override
    public void addReportJob(ReportJobMO reportJobMo) {
        reportJobMo.setDealStaus(SystemConstant.REPORT_JOB_UNTREATED);
        reportJobMo.setCreatedTime(new Date());
        reportJobRepository.insert(reportJobMo);
    }

    @Override
    public ReportJobMO getByJobId(String jobId) {
        // 根据职位id查询举报职位信息，返回结果是Optional类型，如果查询结果不存在，则返回一个空的ReportJobMO对象
        // orElse(new 对象)方法，如果查询结果不存在，则返回一个空的ReportJobMO对象
        // orElseGet(new Supplier<T>)方法，如果Optional中存在值,则返回该值。如果不存在值需要进行额外的处理在返回对象
        return reportJobRepository.findById(jobId).orElseGet(new Supplier<ReportJobMO>() {
            @Override
            public ReportJobMO get() {
                return null;
            }
        });
    }

    @Override
    public PagedGridResult page(ReportJobBo reportJobBo) {
        // 构建查询条件
        Query query = new Query();
        Criteria criteria = new Criteria();
        // 职位名称模糊查询
        if (StringUtils.hasText(reportJobBo.getJobName())) {
            criteria.and("job_name").regex(".*" + reportJobBo.getJobName() + ".*");
        }
        // 公司名称模糊查询
        if (StringUtils.hasText(reportJobBo.getCompanyName())) {
            criteria.and("company_name").regex(".*" + reportJobBo.getCompanyName() + ".*");
        }
        // 处理状态精确查询
        if (reportJobBo.getDealStatus() != 0) {
            criteria.and("deal_status").is(reportJobBo.getDealStatus());
        }
        // 时间范围查询
        Date startTime = reportJobBo.getStartTime();
        Date endTime = reportJobBo.getEndTime();
        if (startTime != null && endTime != null) {
            criteria.and("created_time").gte(startTime).lte(endTime);
        } else if (startTime != null) {
            criteria.and("created_time").gte(startTime);
        } else if (endTime != null) {
            criteria.and("created_time").lte(endTime);
        }
        query.addCriteria(criteria);
        // 查询总记录数
        long count = mongoTemplate.count(query, ReportJobMO.class);
        // 构建分页条件
        Pageable pageable = PageRequest.of(
                reportJobBo.getPageIndex(),
                reportJobBo.getPageSize(),
                Sort.Direction.DESC, "created_time"
        );
        query.with(pageable);
        // 查询当前页数据
        List<ReportJobMO> list = mongoTemplate.find(query, ReportJobMO.class);
        // 封装分页结果
        PagedGridResult result = new PagedGridResult();
        result.setPage(reportJobBo.getPageIndex());
        result.setPageSize(reportJobBo.getPageSize());
        result.setRecords(count);
        result.setRows(list);
        return result;
    }

    @Override
    public void dealReportJobSuccess(String jobId, int dealStatus) {

    }

}
